package com.itcast.googleplay.fragment;


import java.util.List;

import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.itcast.googleplay.R;
import com.itcast.googleplay.adapter.MyBaseAdapter;
import com.itcast.googleplay.domain.AppInfo;
import com.itcast.googleplay.holder.AppHolder;
import com.itcast.googleplay.holder.BaseHolder;
import com.itcast.googleplay.protocol.AppProtocol;
import com.itcast.googleplay.ui.LoadingPage.ResultState;
import com.itcast.googleplay.utils.UIUtils;

public class AppFragment extends BaseFragment {

	private List<AppInfo> appInfoList;
	private PullToRefreshListView prlv;
	private MyAdpater myAdpater;

	@Override
	public ResultState onSubLoad() {
		AppProtocol appProtocol = new AppProtocol();
		appInfoList = appProtocol.getData("app", 0, "");
		if(appInfoList!=null){
			if(appInfoList.size()>0){
				return ResultState.STATE_SUCCESSED;
			}else{
				return ResultState.STATE_EMPTY;
			}
		}
		return ResultState.STATE_ERROR;
	}

	@Override
	public View onCreateSubSuccessedView() {
		View view = UIUtils.inflate(R.layout.layout_listview);
		prlv = (PullToRefreshListView) view.findViewById(R.id.pull_refresh_list);
		ListView listView = prlv.getRefreshableView();
		
		//去掉listView的分割线
		listView.setDivider(null);
		
		myAdpater = new MyAdpater(appInfoList);
		listView.setAdapter(myAdpater);
		
		prlv.setMode(Mode.BOTH);
		prlv.setOnRefreshListener(new OnRefreshListener<ListView>() {
			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				if(prlv.getCurrentMode() == Mode.PULL_FROM_START){
					//刷新
					appInfoList.clear();
					//请求网络获取最新的数据
					subShow();
					//刷新数据适配器
					myAdpater.notifyDataSetChanged();
					//隐藏刷新头
					prlv.onRefreshComplete();
				}else{
					//加载,开启子线程,做下一个数据请求
					new Thread(){
						public void run() {
							//1.20条
							//2.20条	+上一次的20条	40条
							//3.20条+ 上一次40条	60
							
							AppProtocol appProtocol = new AppProtocol();
							List<AppInfo> moreData = appProtocol.getData("app", appInfoList.size(), "");
							if(moreData!=null && moreData.size()>0){
								appInfoList.addAll(moreData);
								
								UIUtils.runInMainThread(new Runnable() {
									@Override
									public void run() {
										//刷新数据适配器
										myAdpater.notifyDataSetChanged();
										//脚底隐藏
										prlv.onRefreshComplete();
									}
								});
							}else{
								UIUtils.runInMainThread(new Runnable() {
									@Override
									public void run() {
										Toast.makeText(UIUtils.getContext(), "没有数据了", 0).show();
										//脚底隐藏
										prlv.onRefreshComplete();
									}
								});
							}
						};
					}.start();
				}
			}
		});
		
		return view;
	}
	
	class MyAdpater extends MyBaseAdapter<AppInfo>{

		public MyAdpater(List<AppInfo> list) {
			super(list);
		}

		@Override
		public BaseHolder getSubBaseholder() {
			return new AppHolder();
		}
		
	}
}
